const express = require('express')
const path = require('path')
const bodyParser = require('body-parser')
const session = require('express-session')

const userRouter = require('./routers/user')
const writeRouter = require('./routers/write')

// 创建服务器
const app = express();

// 配置express art-template中间体
app.engine('.html', require('express-art-template'));
app.set('views', path.join(__dirname, 'views'))
// 配置body-parser
app.use(bodyParser.urlencoded({
  extended: false
}))
app.use(bodyParser.json())
// 配置express-session
app.use(session({
  secret: 'bill', 
  resave: false,
  saveUninitialized: false // 初始化不给session
}))




// 设置静态资源目录
app.use('/node_modules', express.static(path.join(__dirname, 'node_modules')))
app.use('/public', express.static(path.join(__dirname, 'public')))


// 挂载路由
app.use(userRouter);
app.use(writeRouter);


// 未找到路由404
app.use((req, res, next) => {
  res.render('404.html')
})

// err页面
app.use((err, req, res, next) => {
  res.status(500).json({
    err_code: 500,
    message: err.message
  })
})

// 启动
app.listen(8000, () => {
  console.log('app in running ...');
})